package com.zs4sdk.activity.login;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import android.os.Handler;

import com.common.library.util.ActivityHelper;
import com.common.library.util.Helper;
import com.common.library.util.NetworkHelper;
import com.zs4sdk.Zs4GameMenu;
import com.zs4sdk.common.Extra;
import com.zs4sdk.entity.AutoLoginJSONResultEntity;
import com.zs4sdk.entity.Zs4GameData;
import com.zs4sdk.extra.Zs4GameCode;
import com.zs4sdk.common.Zs4GameUtil;

import org.json.JSONObject;

/**
 * Created by Administrator on 2018/4/16.
 */

 abstract class Zs4GameLoginBase implements View.OnClickListener{
    public View mMainView;
    public Activity mActivity;
    protected boolean mIsShow = false;
    protected View.OnClickListener mOnClickListener;
    protected boolean mIsInit = false;
    protected Handler mLoginHandler;
    public static final String LOGINTYPE_EMAIL = "EMAIL";
    public static final String LOGINTYPE_EXCHANGE = "EXCHANGE";
    public Zs4GameLoginBase(Activity act,View view,Handler handler){
        this.mActivity = act;
        mMainView = view;
        mLoginHandler = handler;
      //  init();
    }
    public void setLoginHandler(Handler handler){
        mLoginHandler = handler;
    }
    public void setData(Object data){
    }
    public void setLoginStyle(int style){

    }
    public Object getData(){
        return null;
    }
    public void setOnClickListener(View.OnClickListener onClickListener){
        mOnClickListener = onClickListener;
    }
    public abstract void login();
    protected abstract void show();
    protected abstract void hide();
    public boolean isCanDoNext(){
        return false;
    }
    public abstract void doOnClick(View v);
    protected  void initView(){
    }
    public void handleResult(Bundle bundle){

    }
    public void onClick(View view){
        doOnClick(view);
    }

    public void setError(String error){

    }
    public void showLogin(){
        if(mIsShow)return;
         mIsShow = true;
         show();
         initView();
    }

    public void hideLogin(){
        if(!mIsShow)return;
        mIsShow = false;
        hide();
    }
    /**
     * function: 发送错误信息
     *
     * @param ctx
     * @param action
     * @ author: aXiBa 2013-12-23 下午6:10:15
     */
    protected static void  sendErrorMessage(Context ctx, String action) {

        ActivityHelper.sendBroadcast(action, Zs4GameCode.ERROR, ctx);
    //    Zs4GameMenu.closeActivitysAndShowStartMenu(ctx, action);
    }
    protected   boolean isNetworkAvailable(){
        return NetworkHelper.isNetworkAvailable(mActivity);
    }

    public static  void loginSuccess(Activity activity, String type, JSONObject jsonResult, String account, String password){
        AutoLoginJSONResultEntity entity = new AutoLoginJSONResultEntity(jsonResult);
        final String action = activity.getPackageName().concat(Extra.ACTIONKEY.LOGIN_POSTFIX);
        if (Helper.isNotNull(entity) && entity.isSuccess()) {
            // 设备验证失败, 清空密码让用户重新登录
            if (!Extra.BOOLEAN_TRUE.equals(entity.getVerify())) {
//                        ActivityHelper.showToast(entity.getDesc());

                Zs4GameData.getInstance().clearLoginInfo();
                Zs4GameData.getInstance().clearLastLoginUserInfo();

                //   goLoginOrRegister(ctx, action);
            }
            // 设备验证成功
            else {
                // 同步本地自动分配信
//                        LogUtil.e("========", "设备验证" + type);
                // 手动输入:同步本地的自动分配信息
                if (LOGINTYPE_EMAIL.equals(type)) {
                    Zs4GameMenu.synLocalAutoAssignInfo(entity, account, password);

                } else if (LOGINTYPE_EXCHANGE.equals(type)) {
                    Zs4GameMenu.synLocalAutoAssignInfo(entity);
                }

                Zs4GameMenu.synLocalAutoAssignInfo(entity);
                Zs4GameData.getInstance().setLoginFirstTime(1);
                Zs4GameData.getInstance().saveUserLoginInfoForLoginedSuccess(entity);
           //     Zs4GameMenu.closeActivitysAndShowStartMenu(activity, action);
                if(!entity.isVerify()) {

                }else {


                }
                boolean isSwitchAccount = Zs4GameData.getInstance().saveUserLoginInfoForLoginedSuccess(entity);
                Zs4GameUtil.saveLoginStatus(activity,entity.getUserId(),true);//保存登录状态
                ActivityHelper.sendBroadcast(action, Zs4GameCode.LOGINSUCCESS, Zs4GameCode.LOGINTYPE_EMAIL, activity);
                Zs4GameData.getInstance().setLoginJSONResultEntity(entity);
           //     Zs4GameMenu.startMenu(activity);
                activity.finish();
            }

        }
        // 登录失败
        else {
             if (Helper.isNotEmpty(entity.getDesc())) {
                //设备验证失败

                ActivityHelper.showLongToast(entity.getDesc());

            } else {
                //服务器异常处理
                String error = jsonResult.optString("error");
                if (Helper.isNotEmpty(error)) {
                    ActivityHelper.showLongToast(error);
                }
            }
           sendErrorMessage(activity, action);
            //  goLoginOrRegister(ctx, action);
        }
    }
}
